﻿using BasePlatformCommon;
using BasePlatformNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace L9SubSet
{
    public partial class Form_TimeLockConfig : Form
    {
        private BaseCoreNet baseCoreNet;
        private int subIndex;
        private int portIndex;
        private int miniIndex;

        private List<List<int>> lampindex;

        private string section = "TimeLockConfig";		// 多语言文件配置节名称
        public Form_TimeLockConfig(BaseCoreNet b, int sub, int port, int mini)
        {
            InitializeComponent();
            this.baseCoreNet = b;
            this.subIndex = sub;
            this.miniIndex = mini;
            this.portIndex = port;
            lampindex = new List<List<int>>();
        }
        private LampStatePara checkOneLamp(int checkSubIndex, int checkPortIndex, int checkLampIndex)
        {
            LampStatePara res = null;

            res = baseCoreNet.getLampStatePara(checkSubIndex, checkPortIndex, checkLampIndex);

            return res;
        }

        private void searchLamp()
        {
            lampindex.Clear();
            int totalSubNum = baseCoreNet.getSubControlNum();
            baseCoreNet.setLampCheckProcessing(true);
            Thread.Sleep(100);
            for (int i = 1; i <= totalSubNum; i++)
            {
                baseCoreNet.setControlState(i, true, false);
                Thread.Sleep(300);
                for (int j = 1; j <= 8; j++)
                {
                    List<int> portlamplist = new List<int>();
                    int checkLampIndex = 0;
                    int failNum = 0;
                    bool checkflag = true;
                    while (checkflag)
                    {
                        checkLampIndex++;
                        LampStatePara lampStatePara = checkOneLamp(i, j, checkLampIndex);

                        if (lampStatePara == null)
                        {
                            failNum++;
                            if (failNum == 3)
                            {
                                // 连续3个灯具未检测到, 结束本轮检查
                                failNum = 0;	// 错误计数器归零
                                break;
                            }
                        }
                        else
                        {
                            failNum = 0;
                            portlamplist.Add(j);
                        }

                        Thread.Sleep(30);
                    }
                    lampindex.Add(portlamplist);
                }

                baseCoreNet.setControlState(i, false, false);
                Thread.Sleep(300);
            }
            baseCoreNet.setLampCheckProcessing(false);
        }

        private void nudHour_ValueChanged(object sender, EventArgs e)
        {
            int val = (int)nudHour.Value;
            if (val == -1)
            {
                if (nudDay.Value != 0)
                {
                    nudDay.Value -= 1;
                    nudHour.Value = 23;
                }
                else
                {
                    nudHour.Value = 0;
                }
            }
            if (val == 24)
            {
                if (nudDay.Value != 365)
                {
                    nudDay.Value += 1;
                    nudHour.Value = 0;
                }
                else
                {
                    nudHour.Value = 23;
                }
            }
            if (nudDay.Value == 365)
            {
                if (nudSecond.Value != 0)
                {
                    nudSecond.Value = 0;
                }
                if (nudMinute.Value != 0)
                {
                    nudMinute.Value = 0;
                }
                if (nudHour.Value != 0)
                {
                    nudHour.Value = 0;
                }
            }
        }

        private void nudSecond_ValueChanged(object sender, EventArgs e)
        {
            int val = (int)nudSecond.Value;
            if (val == 0)
            {
                if ((nudMinute.Value == 0) && (nudHour.Value == 0))
                {
                    nudSecond.Value = 1;
                }
            }
            if (val == -1)
            {
                if ((nudMinute.Value == 0) && (nudHour.Value == 0))
                {
                    nudSecond.Value = 1;
                }
                else
                {
                    nudMinute.Value -= 1;
                    nudSecond.Value = 59;
                }
            }
            if (val == 60)
            {
                nudMinute.Value += 1;
                nudSecond.Value = 0;
            }
            if (nudDay.Value == 365)
            {
                if (nudSecond.Value != 0)
                {
                    nudSecond.Value = 0;
                }
                if (nudMinute.Value != 0)
                {
                    nudMinute.Value = 0;
                }
                if (nudHour.Value != 0)
                {
                    nudHour.Value = 0;
                }
            }
        }

        private void nudMinute_ValueChanged(object sender, EventArgs e)
        {
            int val = (int)nudMinute.Value;
            if (val == -1)
            {
                nudHour.Value -= 1;
                nudMinute.Value = 59;
            }
            if (val == 60)
            {
                nudHour.Value += 1;
                nudMinute.Value = 0;
            }
            if (nudDay.Value == 365)
            {
                if (nudSecond.Value != 0)
                {
                    nudSecond.Value = 0;
                }
                if (nudMinute.Value != 0)
                {
                    nudMinute.Value = 0;
                }
                if (nudHour.Value != 0)
                {
                    nudHour.Value = 0;
                }
            }
        }

        private void nudDay_ValueChanged(object sender, EventArgs e)
        {
            if (nudDay.Value == 365)
            {
                if (nudSecond.Value != 0)
                {
                    nudSecond.Value = 0;
                }
                if (nudMinute.Value != 0)
                {
                    nudMinute.Value = 0;
                }
                if (nudHour.Value != 0)
                {
                    nudHour.Value = 0;
                }
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            char c = e.KeyChar;
            if (c == 8) return;
            if (PasswordBox.Text.Length >= 6) e.Handled = true;
            bool validate = false;
            if (('0' <= c) && (c <= '9')) validate = true;
            if (('a' <= c) && (c <= 'z')) validate = true;
            if (('A' <= c) && (c <= 'Z')) validate = true;

            if (!validate) e.Handled = true;
        }

        private void chbShowPassword_CheckedChanged(object sender, EventArgs e)
        {
            PasswordBox.UseSystemPasswordChar = !chbShowPassword.Checked;
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            string password = PasswordBox.Text;
            if (password == "")
            {
                var msg = "请设置密码";
                if (LanguageOper.available)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.0");
                }
                Tools.ShowTipMessage(this, msg);
                return;
            }
            int timesecond = (int)nudDay.Value * 79800 + (int)nudHour.Value * 3600 + (int)nudMinute.Value * 60 + (int)nudSecond.Value;
            if (timesecond == 0)
            {
                var msg = "请设置时间";
                if (LanguageOper.available)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.1");
                }
                Tools.ShowTipMessage(this, msg);
                return;
            }
            byte[] passByte = new byte[64];
            for (int i = 0; i < 64; i++)
            {
                passByte[i] = 0;
            }
            if (password == "")
            {
                var msg = "请设置密码";
                if (LanguageOper.available)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.0");
                }
                Tools.ShowTipMessage(this, msg);
                return;
            }
            if (password.Length != 6)
            {
                var msg = "密码必须为6位";
                if (LanguageOper.available)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.2");
                }
                Tools.ShowTipMessage(this, msg);
                return;
            }
            for (int i = 0; i < password.Length; i++)
            {
                passByte[i] = (byte)password[i];
            }

            //int totalCount = 0;
            //int succeedCount = 0;
            //int failCount = 0;

            btnSet.Enabled = false;
            btnRemove.Enabled = false;
            //searchLamp();
            //int totalSubNum = baseCoreNet.getSubControlNum();
            //int subStart = subIndex;
            //int subEnd = subIndex;
            //if (subIndex == 0)
            //{
            //    subStart = 1;
            //    subEnd = totalSubNum;
            //}
            //int portStart = portIndex;
            //int portEnd = portIndex;
            //if (portIndex == 0)
            //{
            //    portStart = 1;
            //    portEnd = 8;
            //}
            //for (int i = subStart; i <= subEnd; i++)
            //{
            //    for (int j = portStart; j < portEnd; j++)
            //    {
            //        foreach (int lamp in lampindex[(i-1) * 8 + (j-1)])
            //        {
            //            bool res = baseCoreNet.setTimeLock(i, j, lamp, timesecond, passByte);
            //            if (res)
            //            {
            //                succeedCount++;
            //            }
            //            else
            //            {
            //                failCount++;
            //            }
            //            totalCount++;
            //        }
            //    }
            //}

            baseCoreNet.setTimeLockLite(subIndex, subIndex, miniIndex, timesecond, passByte);

            btnSet.Enabled = true;
            btnRemove.Enabled = true;
            //MessageBox.Show(String.Format("时间锁设置完毕，共设置{0}个，成功{1}个，失败{2}个", totalCount, succeedCount, failCount));
            var msg1 = "时间锁设置完毕";
            if (LanguageOper.available)
            {
                msg1 = LanguageOper.ReadValue(section, "msgStr.3");
            }
            Tools.ShowTipMessage(this, msg1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string password = PasswordBox.Text;
            if (password == "")
            {
                var msg = "请设置密码";
                if (LanguageOper.available)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.0");
                }
                Tools.ShowTipMessage(this, msg);
                return;
            }
            byte[] passByte = new byte[64];
            for (int i = 0; i < 64; i++)
            {
                passByte[i] = 0;
            }
            
            for (int i = 0; i < password.Length; i++)
            {
                passByte[i] = (byte)password[i];
            }

            //int totalCount = 0;
            //int succeedCount = 0;
            //int failCount = 0;

            btnSet.Enabled = false;
            btnRemove.Enabled = false;
            //searchLamp();
            //int totalSubNum = baseCoreNet.getSubControlNum();
            //int subStart = subIndex;
            //int subEnd = subIndex;
            //if (subIndex == 0)
            //{
            //    subStart = 1;
            //    subEnd = totalSubNum;
            //}
            //int portStart = portIndex;
            //int portEnd = portIndex;
            //if (portIndex == 0)
            //{
            //    portStart = 1;
            //    portEnd = 8;
            //}
            //for (int i = subStart; i <= subEnd; i++)
            //{
            //    for (int j = portStart; j < portEnd; j++)
            //    {
            //        foreach (int lamp in lampindex[(i - 1) * 8 + (j - 1)])
            //        {
            //            bool res = baseCoreNet.removeTimeLock(i, j, lamp, passByte);
            //            if (res)
            //            {
            //                succeedCount++;
            //            }
            //            else
            //            {
            //                failCount++;
            //            }
            //            totalCount++;
            //        }
            //    }
            //}

            baseCoreNet.removeTimeLockLite(subIndex, portIndex, miniIndex, passByte);

            btnSet.Enabled = true;
            btnRemove.Enabled = true;
            //MessageBox.Show(String.Format("时间锁清除完毕，共清除{0}个，成功{1}个，失败{2}个", totalCount, succeedCount, failCount));
            var msg1 = "时间锁清除完毕";
            if (LanguageOper.available)
            {
                msg1 = LanguageOper.ReadValue(section, "msgStr.4");
            }
            Tools.ShowTipMessage(this, msg1);
        }

        public void setLanguageText()
        {
            this.Text = LanguageOper.ReadValue(section, "TimeLockConfig.Text");
            grpTime.Text = LanguageOper.ReadValue(section, "grpTime.Text");
            lblDay.Text = LanguageOper.ReadValue(section, "lblDay.Text");
            lblHour.Text = LanguageOper.ReadValue(section, "lblHour.Text");
            lblMin.Text = LanguageOper.ReadValue(section, "lblMin.Text");
            lblSec.Text = LanguageOper.ReadValue(section, "lblSec.Text");
            lblPassword.Text = LanguageOper.ReadValue(section, "lblPassword.Text");
            chbShowPassword.Text = LanguageOper.ReadValue(section, "chbShowPassword.Text");
            btnSet.Text = LanguageOper.ReadValue(section, "btnSet.Text");
            btnRemove.Text = LanguageOper.ReadValue(section, "btnRemove.Text");

            if (LanguageOper.LocalLanguage == LanguageEnum.EN)
            {
                chbShowPassword.Left -= 20;
            }
        }
    }
}
